<?php


namespace App\Http\Controllers\Api\V1;

use App\Models\Curriculum;
use App\Models\KeywordOne;
use App\Models\KeywordTwo;
use App\Models\MaterialUnit;
use App\Models\NaturalSpelling;
use App\Models\Paint;
use App\Models\TeachingMaterial;
use App\Models\TestBook;
use App\Models\Video;

class BookController extends BaseController
{
    public function lists()
    {
        $student = auth()->user();
        $class = $student->classes;
        $bookId = json_decode($class->teaching_material, true);
        if (!empty($bookId)) {
            $bookList = TeachingMaterial::whereIn('id', $bookId)->get(['id', 'name']);
            $result = [];
            $unitSelect = [
                'id',
                'video_id',
                'keyword_one_id',
                'keyword_two_id',
                'paint_id',
                'natural_spelling_id',
                'test_id'
            ];
            foreach ($bookList as $item) {
                $units = $item->units()->orderBy('id', 'asc')->get($unitSelect);
                $unitList = [];
                foreach ($units as $key => $unit) {
                    $list['id'] = $unit->id;
                    $list['name'] = 'unit-'. ($key + 1);
                    $keywordOne = $unit->keyword_one_id == 0 ? [] :
                        KeywordOne::select('data')->find($unit->keyword_one_id);
                    if (!is_array($keywordOne) && $keywordOne != null) {
                        $keywordOne = json_decode($keywordOne->data, true);
                        foreach ($keywordOne as $k => $one) {
                            $keywordOne[$k]['pic'] = env('APP_URL'). '/uploads/' .  $one['pic'];
                            $keywordOne[$k]['audio'] = env('APP_URL'). '/uploads/' .  $one['audio'];
                        }
                    }
                    $list['keyword_one'] = $keywordOne;
                    $keywordTwo = $unit->keyword_two_id == 0 ? [] :
                        KeywordTwo::select('data')->find($unit->keyword_two_id);
                    if (!is_array($keywordTwo) && $keywordTwo != null) {
                        $keywordTwo = json_decode($keywordTwo->data, true);
                        foreach ($keywordTwo as $k => $two) {
                            $keywordTwo[$k]['pic'] = env('APP_URL'). '/uploads/' .  $two['pic'];
                            $keywordTwo[$k]['audio'] = env('APP_URL'). '/uploads/' .  $two['audio'];
                        }
                    }
                    $list['keyword_two'] = $keywordTwo;

                    $paint = $unit->paint_id == 0 ? [] : Paint::select('cover', 'data')->find($unit->paint_id);
                    if (!is_array($paint) && $paint != null) {
                        $paints = json_decode($paint->data, true);
                        foreach ($paints as $k => $pa) {
                            $paints[$k]['key'] = env('APP_URL'). '/uploads/' .  $pa['key'];
                            $paints[$k]['pic'] = env('APP_URL'). '/uploads/' .  $pa['pic'];
                            $paints[$k]['audio'] = env('APP_URL'). '/uploads/' .  $pa['audio'];
                        }
                        $paintsList['cover'] = env('APP_URL'). '/uploads/' . $paint->cover;
                        $paintsList['data'] = $paints;
                    } else {
                        $paintsList = [];
                    }
                    $list['paint'] = $paintsList;

                    $video = $unit->video_id == 0 ? [] : Video::select('video_one', 'video_two')->find($unit->video_id);
                    if (!is_array($video) && $video != null) {
                        $video->video_one = env('APP_URL'). '/uploads/' . $video->video_one;
                        $video->video_two = env('APP_URL'). '/uploads/' . $video->video_two;
                    }
                    $list['video'] = $video;

                    $naturalSpelling = $unit->natural_spelling_id == 0 ? [] :
                        NaturalSpelling::select('data')->find($unit->natural_spelling_id);
                    if (!is_array($naturalSpelling) && $naturalSpelling != null) {
                        $naturalSpelling = $naturalSpelling->data;
                        foreach ($naturalSpelling as $k => $na) {
                            $naturalSpelling[$k]['pic'] = env('APP_URL'). '/uploads/' .  $na['pic'];
                            $naturalSpelling[$k]['mp3'] = env('APP_URL'). '/uploads/' .  $na['mp3'];
                            foreach ($na['syllable'] as $ke => $n) {
                                $naturalSpelling[$k]['syllable'][$ke]['pronunciation'] = env('APP_URL'). '/uploads/' . $n['pronunciation'];
                            }
                        }
                    }
                    $list['nature'] = array_values($naturalSpelling);

                    $test = $unit->test_id == 0 ? [] :
                        TestBook::select('data')->find($unit->test_id);
                    if (!is_array($test) && $test != null) {
                        $test = $test->data;
                        foreach ($test as $k => $te) {
                            $test[$k]['mp3'] = env('APP_URL'). '/uploads/' .  $te['mp3'];
                        }
                    }
                    $list['test'] = array_values($test);

                    $unitList[] = $list;
                }
                $result[] = [
                    'id' => $item->id,
                    'name' => $item->name,
                    'unit' => $unitList,
                ];
            }
        } else {
            $result = [];
        }
        return response()->json(['list' => $result]);
    }

    public function curriculum()
    {
        $student = auth()->user();
        $classId = $student->classes->id;
        $curriculum = Curriculum::where('class_id', $classId)->where('dated', date('Y-m-d'))->first();
        if ($curriculum != null) {
            $unit = MaterialUnit::find($curriculum->unit_id);
            switch ($curriculum->link) {
                case 1:
                    $keywordOne = KeywordOne::select('data')->find($unit->keyword_one_id);
                    $result['name'] = '单词一';
                    $keywordOne = json_decode($keywordOne->data, true);
                    foreach ($keywordOne as $k => $one) {
                        $keywordOne[$k]['pic'] = env('APP_URL'). '/uploads/' .  $one['pic'];
                        $keywordOne[$k]['audio'] = env('APP_URL'). '/uploads/' .  $one['audio'];
                    }
                    $result['data'] = $keywordOne;
                    break;
                case 2:
                    $keywordTwo = $unit->keyword_two_id == 0 ? [] :
                        KeywordTwo::select('data')->find($unit->keyword_two_id);
                    $result['name'] = '单词二';
                    if (!is_array($keywordTwo) && $keywordTwo != null) {
                        $keywordTwo = json_decode($keywordTwo->data, true);
                        foreach ($keywordTwo as $k => $two) {
                            $keywordTwo[$k]['pic']   = env('APP_URL') . '/uploads/' . $two['pic'];
                            $keywordTwo[$k]['audio'] = env('APP_URL') . '/uploads/' . $two['audio'];
                        }
                    }
                    $result['data'] = $keywordTwo;
                    break;
                case 3:
                    $video = $unit->video_id == 0 ? [] : Video::select('video_one', 'video_two')->find($unit->video_id);
                    $result['name'] = '视频';
                    if (!is_array($video) && $video != null) {
                        $video->video_one = env('APP_URL'). '/uploads/' . $video->video_one;
                        $video->video_two = env('APP_URL'). '/uploads/' . $video->video_two;
                    }
                    $result['data'] = $video;
                    break;
                case 4:
                    $result['name'] = '音乐';
                    $result['data'] = [];
                    break;
                case 5:
                    $result['name'] = '自然拼读';
                    $naturalSpelling = $unit->natural_spelling_id == 0 ? [] :
                        NaturalSpelling::select('data')->find($unit->natural_spelling_id);
                    if (!is_array($naturalSpelling) && $naturalSpelling != null) {
                        $naturalSpelling = $naturalSpelling->data;
                        foreach ($naturalSpelling as $k => $na) {
                            $naturalSpelling[$k]['pic'] = env('APP_URL'). '/uploads/' .  $na['pic'];
                            $naturalSpelling[$k]['mp3'] = env('APP_URL'). '/uploads/' .  $na['mp3'];
                            foreach ($na['syllable'] as $ke => $n) {
                                $naturalSpelling[$k]['syllable'][$ke]['pronunciation'] = env('APP_URL'). '/uploads/' . $n['pronunciation'];
                            }
                        }
                    }
                    $result['data'] = array_values($naturalSpelling);
                    break;
                case 6:
                    $result['name'] = '测试';
                    $test = $unit->test_id == 0 ? [] :
                        TestBook::select('data')->find($unit->test_id);
                    if (!is_array($test) && $test != null) {
                        $test = $test->data;
                        foreach ($test as $k => $te) {
                            $test[$k]['mp3'] = env('APP_URL'). '/uploads/' .  $te['mp3'];
                        }
                    }
                    $result['data'] = array_values($test);
                    break;
                case 7:
                    $result['name'] = '绘本';
                    $paint = $unit->paint_id == 0 ? [] : Paint::select('cover', 'data')->find($unit->paint_id);
                    if (!is_array($paint) && $paint != null) {
                        $paints = json_decode($paint->data, true);
                        foreach ($paints as $k => $pa) {
                            $paints[$k]['key'] = env('APP_URL'). '/uploads/' .  $pa['key'];
                            $paints[$k]['pic'] = env('APP_URL'). '/uploads/' .  $pa['pic'];
                            $paints[$k]['audio'] = env('APP_URL'). '/uploads/' .  $pa['audio'];
                        }
                        $paintsList['cover'] = env('APP_URL'). '/uploads/' . $paint->cover;
                        $paintsList['data'] = $paints;
                    } else {
                        $paintsList = [];
                    }
                    $result['data'] = $paintsList;
                    break;
                default:
                    $result = [];
                    break;
            }
        } else {
            $result = [];
        }

        return response()->json(['list' => $result]);
    }
}
